package org.dromara.customer.controller;

import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.customer.domain.vo.TaOrdersVo;
import org.dromara.customer.domain.bo.TaOrdersBo;
import org.dromara.customer.service.ITaOrdersService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 订单
 * 前端访问路由地址为:/customer/orders
 *
 * @author xiyin321
 * @date 2024-07-09
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/orders")
public class TaOrdersController extends BaseController {

    private final ITaOrdersService taOrdersService;

    /**
     * 查询订单列表
     */
    @GetMapping("/list")
    public TableDataInfo<TaOrdersVo> list(TaOrdersBo bo, PageQuery pageQuery) {
        return taOrdersService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出订单列表
     */
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TaOrdersBo bo, HttpServletResponse response) {
        List<TaOrdersVo> list = taOrdersService.queryList(bo);
        ExcelUtil.exportExcel(list, "订单", TaOrdersVo.class, response);
    }

    /**
     * 获取订单详细信息
     *
     * @param ordersId 主键
     */
    @GetMapping("/{ordersId}")
    public R<TaOrdersVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long ordersId) {
        return R.ok(taOrdersService.queryById(ordersId));
    }

    /**
     * 新增订单
     */
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TaOrdersBo bo) {
        return toAjax(taOrdersService.insertByBo(bo));
    }

    /**
     * 修改订单
     */
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TaOrdersBo bo) {
        return toAjax(taOrdersService.updateByBo(bo));
    }

    /**
     * 删除订单
     *
     * @param ordersIds 主键串
     */
    @Log(title = "订单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ordersIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ordersIds) {
        return toAjax(taOrdersService.deleteWithValidByIds(List.of(ordersIds), true));
    }
}
